/*
 * @Author: zwc 6537397+uni-yunApp@user.noreply.gitee.com
 * @Date: 2023-11-24 15:14:29
 * @LastEditors: zwc 6537397+uni-yunApp@user.noreply.gitee.com
 * @LastEditTime: 2023-12-14 11:00:13
 * @FilePath: \covue-main\lib\core\compile.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const path = require('path')
const ejs = require('ejs')
const fs = require('fs')
// 转为promise
const { promisify } = require('util');
const renderFile = promisify(ejs.renderFile);
// 解析模板
const pageCompile = (template, props, dest) => {
  const templatePath = path.resolve(__dirname, `../template/${template}`)
  const pagePath =props.isNeed? path.resolve(`${props.lowerName}`, `${props.lowerName}.vue`):path.resolve(`${props.lowerName}.vue`)
  
  renderFile(templatePath, { props }, {}).then(result => {
    templateInput(pagePath, result)
  }).catch(err => {
    console.log(err);
  })
}
// 递归判断文件夹是否存在，如果不存在，则创建文件夹
const makeDir = (dirPath) => {
  if (fs.existsSync(dirPath)) {
    return true;
  } else {
    if (makeDir(path.dirname(dirPath))) {
      fs.mkdirSync(dirPath)
      return true;
    }
  }
}
// 写入
const templateInput = (path, content) => {
  return fs.promises.writeFile(path, content)
}

module.exports = {
  pageCompile,
  makeDir,
 
}